package com.zzwtec.information.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.zzwtec.information.dao.BaseDao;
import com.zzwtec.interfaces.Page;
import com.zzwtec.information.model.Ad;

@Repository(value = "adDao")
public class AdDao extends BaseDao<Ad>{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	private final String TABLE_NAME = "ad";
	
	
	/**
	 * 分页查询
	 * @param page
	 * @return 数据模型列表
	 */
	@Transactional(readOnly = true)
	public Page<Ad> selectPage(Page<Ad> page) {
		return super.selectPage(Ad.class, TABLE_NAME, page, new AdRowMapper());
	}
	
	/**
	 * 全查询，一般不推荐使用，除非数据量不大
	 * 
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<Ad> selectAll() {		
		return super.selectAll(TABLE_NAME, new AdRowMapper());
	}

	/**
	 * 获取一条记录
	 * 
	 * @param id
	 * @return
	 */
	@Transactional(readOnly = true)
	public Ad selectOne(String id) {		
		return super.selectOne(TABLE_NAME, id, new AdRowMapper());

	}

	

	

	/**
	 * 更新单表数据
	 * 
	 * @param sql
	 * @return
	 */
	public int update(String sql) {
		return jdbcTemplate.update(sql);
	}

	/**
	 * 批量更新单表数据
	 * 
	 * @param sql
	 * @param batchArgs
	 * @return
	 */
	public int[] batchUpdate(String sql) {
		return jdbcTemplate.batchUpdate(sql);
	}

	/**
	 * 更新区域
	 * @param bean
	 * @return
	 */
	public int update(Ad bean) {
		String sql = "update ad set name= ?,type= ?,timelength= ?,ad_level= ?,play_time= ?,size= ?,fabu= ?,user_type= ?,create_id= ?,on_time= ?,off_time= ?,files= ?,adtext= ? where id= ?"; 
		Object[] params = new Object[]{bean.getName(),bean.getType(),bean.getTimelength(),bean.getAdLevel(),bean.getPlayTime(),bean.getSize(),bean.isFabu(),bean.getUserType(),bean.getCreateId(),bean.getOnTime(),bean.getOffTime(),bean.getFiles(),bean.getAdtext(),bean.getId()};;
		return update(sql, params);
	}
	
	/**
	 * 更新单表数据
	 * 
	 * @param sql
	 * @param params
	 * @return
	 */
	public int update(String sql, Object... params) {
		return jdbcTemplate.update(sql, params);
	}

	/**
	 * 批量更新单表数据,支持sql预处理语句参数
	 * 
	 * @param sql
	 * @param batchArgs
	 * @return
	 */
	public int[] batchUpdate(String sql, List<Object[]> batchArgs) {
		return jdbcTemplate.batchUpdate(sql, batchArgs);
	}

	/**
	 * 添加区域
	 * @param bean
	 * @return
	 */
	public int insert(Ad bean) {
		String sql = "insert into ad (id,name,type,timelength,ad_level,play_time,size,fabu,user_type,create_id,on_time,off_time,files,adtext) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		Object[] params = new Object[]{bean.getId(),bean.getName(),bean.getType(),bean.getTimelength(),bean.getAdLevel(),bean.getPlayTime(),bean.getSize(),bean.isFabu(),bean.getUserType(),bean.getCreateId(),bean.getOnTime(),bean.getOffTime(),bean.getFiles(),bean.getAdtext()};;		
		return insert(sql, params);
	}
	
	

	/**
     * 按id删除数据
     * @param id
     * @return
     */
	public int deleteById(String id) {
		String sql = "delete from ad where id = ?";		
		Object[] params = new Object[]{id};
		return delete(sql, params);
	}
	
	/**
	 * 按ids删除数据
     * @param ids
     * @return 受影响行数
	 */
	public int deleteByIds(String[] ids) {
		return super.deleteByIds(TABLE_NAME, ids);
	}
	
	/**
	 * 删除数据 
	 * @param sql
	 * @param params
	 * @return
	 */
	private int delete(String sql, Object... params) {
		if (sql == null || !sql.startsWith("delete")) {
			throw new RuntimeException("不是合法的删除数据sql语句: " + sql);
		}
		return jdbcTemplate.update(sql, params);
	}

}

class AdRowMapper implements RowMapper<Ad> {
	@Override
	public Ad mapRow(ResultSet rs, int rowNum) throws SQLException {
		Ad bean = new Ad();		
		bean.setId(rs.getString("id"));
		bean.setName(rs.getString("name"));
		bean.setType(rs.getInt("type"));
		bean.setTimelength(rs.getInt("timelength"));
		bean.setAdLevel(rs.getInt("ad_level"));
		bean.setPlayTime(rs.getString("play_time"));
		bean.setSize(rs.getLong("size"));
		bean.setFabu(rs.getBoolean("fabu"));
		bean.setUserType(rs.getInt("user_type"));
		bean.setCreateId(rs.getString("create_id"));
		bean.setOnTime(rs.getLong("on_time"));
		bean.setOffTime(rs.getLong("off_time"));
		bean.setFiles(rs.getString("files"));
		bean.setAdtext(rs.getString("adtext"));
		return bean;
	}

}